from django.shortcuts import render, redirect
from Bugs import models


def bug_list(request):
    queryset = models.Bug.objects.all()
    return render(request, 'bug_list.html', {'queryset': queryset})


def bug_add(request):
    if request.method == 'GET':
        return render(request, 'user_add.html')

    title = request.POST.get('title')
    models.UserInfo.objects.create(title=title)
    queryset = models.UserInfo.objects.all()
    return render(request, 'user_list.html', {'queryset': queryset})


def bug_edit(request, nid):
    if request.method == 'GET':
        row_object = models.UserInfo.objects.filter(id=nid).first()
        return render(request, 'user_edit.html', {'row_object': row_object})
    models.UserInfo.objects.filter(id=nid).update(username=request.POST.get('username'))
    models.UserInfo.objects.filter(id=nid).update(password=request.POST.get('password'))
    return redirect("/user/list/")


def bug_delete(request):
    nid = request.GET.get('nid')
    models.UserInfo.objects.filter(id=nid).delete()
    return redirect("/user/list/")
